//
//  VC_CourseAllComment.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/25.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_CourseAllComment.h"
#import "Cell_Comment.h"
#import "Model_AllComment.h"
#import "VC_Comment.h"

static NSString *identifier = @"cell_course_all_comment_identifier";
@interface VC_CourseAllComment ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation VC_CourseAllComment

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
}
- (instancetype)init {
    if (self = [super init]) {
        self = [[UIStoryboard storyboardWithName:@"CourseListStoryBoard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([self class])];
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSArray *)dataArray {
    if (!_dataArray) {
        Model_AllComment *model0 = [[Model_AllComment alloc] init];
        Model_AllComment *model1 = [[Model_AllComment alloc] init];
        Model_AllComment *model2 = [[Model_AllComment alloc] init];
        Model_AllComment *model3 = [[Model_AllComment alloc] init];
        Model_AllComment *model4 = [[Model_AllComment alloc] init];
        
        model0.headUrl = @"";
        model1.headUrl = @"";
        model3.headUrl = @"";
        model4.headUrl = @"";
        
        model0.userName = @"张三";
        model1.userName = @"李四";
        model2.userName = @"王二";
        model3.userName = @"陈胜";
        model4.userName = @"吴广";
        
        model0.stars = 1.0;
        model1.stars = 2.0;
        model2.stars = 3.0;
        model3.stars = 4.0;
        model4.stars = 5.0;
        
        model0.timeInterval = 3;
        model1.timeInterval = 4;
        model2.timeInterval = 5;
        model3.timeInterval = 6;
        model4.timeInterval = 7;
        
        
        model0.repliesCount = 0;
        model0.repliesCount = 0;
        model0.repliesCount = 0;
        model0.repliesCount = 3; // 对应评论条数 // 是否已评论
        model0.repliesCount = 0;
        
        model0.praiseCount = 0;
        model1.praiseCount = 0;
        model2.praiseCount = 0;
        model3.praiseCount = 3; // 对应是否已赞
        model4.praiseCount = 0;
        
        
        model0.comments = @"这是实战派的运营专家，未来所有产品都免不了成为垂直的社区，到底怎么做，最小的颗粒是什么，讲的太清楚了。";
        model1.comments = @"我很欣赏老师说的对于人性的把握，以及对于痛点的挖掘，其实我们太多的产品太容易复制所谓的商业模式，但对于一些深层运营缺乏长久的规划，就像传统行业的竞争，往往很多照搬了模式，却很多无法去挖掘人性和痛点，这是否也可以理解为传统的创新更要难于我们现在互联网的创新。";
        model2.comments = @"干货，对于社区的认识更加清晰了！";
        model3.comments = @"老师讲的很棒，对我们做运营的很有启发\n为什么我们不喜欢参与？为什么我们不喜欢去完成你们的作业，小伙伴们是否也从课程有了启发？？";
        model4.comments = @"干货，对于这个社区认识更加清晰了！";
        
        model0.commentsFont = [UIFont systemFontOfSize:13];
        model1.commentsFont = [UIFont systemFontOfSize:13];
        model2.commentsFont = [UIFont systemFontOfSize:13];
        model3.commentsFont = [UIFont systemFontOfSize:13];
        model4.commentsFont = [UIFont systemFontOfSize:13];
        
        
        NSString *name = @"欧阳彦：";
        NSString *name0 = @"张潇芷：";
        NSString *name1 = @"空白篇篇";
        
        NSString *s = [NSString stringWithFormat:@"%@下了吗\n%@听完了非常棒，下载了分享给大家，有要的可以加我微信WJYYAT\n%@回复%@哈哈", name, name0, name1, name0];
        NSMutableAttributedString *mAttri = [[NSMutableAttributedString alloc] initWithString:s];
        [mAttri addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11], NSForegroundColorAttributeName:MAIN_COLOR} range:[s rangeOfString:name]];
        [mAttri addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11], NSForegroundColorAttributeName:MAIN_COLOR} range:[s rangeOfString:name0]];
        [mAttri addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11], NSForegroundColorAttributeName:MAIN_COLOR} range:[s rangeOfString:name1]];
        
        model0.replies = [[NSAttributedString alloc] initWithString:@""];
        model1.replies = [[NSAttributedString alloc] initWithString:@""];
        model2.replies = [[NSAttributedString alloc] initWithString:@""];
        model3.replies = [[NSAttributedString alloc] initWithAttributedString:mAttri];
        model4.replies = [[NSAttributedString alloc] initWithString:@""];
        
        
        _dataArray = @[@[model0,
                         model1],
                       @[model2,
                         model3,
                         model4]];
    }
    return _dataArray;
}

/**
 初始化tableView
 */
- (void)initTableView {
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Cell_Comment class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
}


#pragma mark --mark UITableViewDelegate UItableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_AllComment *model = self.dataArray[indexPath.section][indexPath.row];
    Cell_Comment *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[Cell_Comment alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    __weak typeof(self) weakself = self;
    [cell refreshWithModel:model CallBackBlock:^(CellCommentButtonClickType type) {
        [weakself commentCellButtonClickCallBackWith:type];
    }];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = self.dataArray[section];
    return arr.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    @autoreleasepool {
        if (section == 0) {
            // 精选评价
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.mj_w, 40.0f)];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, view.mj_w - 20, 40.0f)];
            label.text = @"精选评价";
            label.font = [UIFont systemFontOfSize:17];
            [view addSubview:label];
            return view;
        }else {
            // 全部评价
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.mj_w, 40.0f)];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, view.mj_w - 20, 40.0f)];
            label.text = @"全部评价";
            label.font = [UIFont systemFontOfSize:17];
            [view addSubview:label];
            return view;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_AllComment *model = self.dataArray[indexPath.section][indexPath.row];
    return model.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 10.0f;
    }else {
        return CGFLOAT_MIN;
    }
}


#pragma mark --mark EVENT

- (void)commentCellButtonClickCallBackWith:(CellCommentButtonClickType)type {
    switch (type) {
        case CellCommentButtonClickTypeMeesage: {
            // 评论信息
            [self comment];
            break;
        }
        case CellCommentButtonClickTypePraise: {
            // 点赞
            
            break;
        }
        default:
            break;
    }
}

- (void)comment{
    VC_Comment *vc = [[VC_Comment alloc] init];
    vc.title = @"回复";
    [self.navigationController pushViewController:vc animated:YES];
}
@end
